import { defineStore } from "pinia";
import router from "@/router/index";

export const useRouteStore = defineStore("route", {
    state: () => ({
        cacheRoutes: [],
    }),
    actions: {
        //添加路由
        addRoutes(route) {
            if (this.cacheRoutes.some((v) => v.fullPath === route.fullPath))
                return;
            this.cacheRoutes.push(route);
        },

        //关闭路由
        closeRoutes(path, fullPath) {
            const { cacheRoutes } = this;
            let list = cacheRoutes.filter((v) => v.fullPath !== path);
            if (path === fullPath) {
                router.replace({
                    name: list[list.length - 1].name,
                });
            }
            this.cacheRoutes = list;
        },
    },
});
